home *** CD-ROM | disk | FTP | other *** search
- Path: newsbf02.news.aol.com!not-for-mail
- From: residue@aol.com (Residue)
- Newsgroups: comp.lang.c++
- Subject: [Q] Friends of nested classes
- Date: 8 Jan 1996 19:40:39 -0500
- Organization: America Online, Inc. (1-800-827-6364)
- Sender: root@newsbf02.news.aol.com
- Message-ID: <4csdi7$afa@newsbf02.news.aol.com>
- Reply-To: residue@aol.com (Residue)
- NNTP-Posting-Host: newsbf02.mail.aol.com
-
- How does one define a friend function of a nested class, taking an operand
- of that class, outside the class declaration? For example, this code
- compiles just fine (with MS Visual C++ 4.0, at least):
-
- #include <Foo.h> // class Foo { };
- #include <Bar.h> // class Bar { class Imp; };
-
- class Bar::Imp
- {
- friend Foo& operator+=(Foo& f, Imp i) { return f; } // OK
- };
-
- but when I move the function definition outside the declaration, it fails:
-
- #include <Foo.h> // class Foo { };
- #include <Bar.h> // class Bar { class Imp; };
-
- class Bar::Imp
- {
- friend Foo& operator+=(Foo& f, Imp i);
- };
-
- Foo& operator+=(Foo& f, Bar::Imp i) { return f; } // wrong(?)
-
- Should it? I don't want to change the header files, so I'm using helper
- member functions as a workaround at the moment, as in:
-
- #include <Foo.h> // class Foo { };
- #include <Bar.h> // class Bar { class Imp; };
-
- class Bar::Imp
- {
- static Foo& h(Foo& f, Imp i);
- friend Foo& operator+=(Foo& f, Imp i) { return h(f, i); }
- };
-
- Foo& Bar::Imp::h(Foo& f, Bar::Imp i) { return f; } // OK
-
- but I'd rather not if I don't have to. What's the RIGHT way to do this?
-
-
- Bryan Bowe
- residue@aol.com
-